package com.windfone.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.windfone.model.ProductType;
import com.windfone.service.ProductTypeService;

public class ProductTag extends TagSupport {
	private String typeName;
	private ProductTypeService productTypeService;
	private List<ProductType> types;
	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
 
	public String getTypeName() {
		return typeName;
	}
	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
 
 
	public int doStartTag() throws JspException {
		ServletContext sc = pageContext.getServletContext();
		if ( productTypeService == null )
			productTypeService = (ProductTypeService) WebApplicationContextUtils.getWebApplicationContext(sc).getBean("productTypeService");
		if ( types ==  null )
			types = productTypeService.listProductType();
//		return(SKIP_BODY);
		try {
			String typeId = "";
			for( int i=0;i<types.size();i++){
				if ( types.get(i).getTypeName().equals(typeName)){
					typeId = typeId + types.get(i).getId();	
					break;
				}
			}
			pageContext.getOut().print(	typeId );
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	
	public void setProductTypeService(ProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}
	public ProductTypeService getProductTypeService() {
		return productTypeService;
	}
}
